﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chenxin.Lib.Passport.BLL
{
    //*******************************************
    //功能概要       ：获得会话中使用的用户信息
    //参数説明       ：无
    //返回值説明     ：用户信息
    //功能詳細       ：先从Cache中查找，没有的情况下，从数据库中查找
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-02 
    //*******************************************
    [Serializable]
    public class Cache_UserInfo
    {
        public Cache_UserInfo(Guid strUserId)
        {
            this._userId = strUserId;
        }

        public Cache_UserInfo(){}

        //用户标示
        private Guid _userId;
        //工号
        private string _userCode;
        //用户名
        private string _userName;
        //城市
        private Guid _cityId;
        //城市名称
        private string _cityName;
        //所属部门ID
        private Guid _departId;
        //所属部门名
        private string _departName;
        private Guid _companyId;
        private string _companyName;

        //用户类型
        private int _usertype;

        /// <summary>
        /// 用户所属公司ID
        /// </summary>
        public Guid CompanyId
        {
            get { return _companyId; }
            set { _companyId = value; }
        }

        /// <summary>
        /// 用户所属公司
        /// </summary>
        public string CompanyName
        {
            get { return _companyName; }
            set { _companyName = value; }
        }

        /// <summary>
        /// 用户类型
        /// </summary>
        public int UserType
        {
            get { return _usertype; }
            set { _usertype = value; }
        }
        //用户标示属性
        public Guid UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        //工号属性
        public string UserCode
        {
            get { return _userCode; }
            set { _userCode = value; }
        }
        //用户名属性
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        //城市属性
        public Guid CityId
        {
            get { return _cityId; }
            set { _cityId = value; }
        }
        //城市名属性
        public string CityName
        {
            get { return _cityName; }
            set { _cityName = value; }
        }
        //所属部门ID属性
        public Guid DepartId
        {
            get { return _departId; }
            set { _departId = value; }
        }
        //所属部门名属性
        public string DepartName
        {
            get { return _departName; }
            set { _departName = value; }
        }

        /// <summary>
        /// 通过UserId得到一个对象实体
        /// </summary>
        public Cache_UserInfo GetCurrentUserInfo()
        {
            //Cache服务类
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            //用户基本信息
            Cache_UserInfo csCustInfo;
            //用户信息键
            string strKeyUser = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_USER, UserId.ToString());
            //从Cache中查找
            csCustInfo = cs.GetObject<Cache_UserInfo>(strKeyUser);
            //未命中
            if (!cs.Hit)
            {
                //用户基本信息类
                Chenxin.Lib.Passport.Entity.Hr_UserInfo entUserInfo;

                csCustInfo = new Cache_UserInfo();
                //根据用户Id取得用户基本信息
                entUserInfo = Chenxin.Lib.Passport.DAL.Hr_UserInfo.GetEntityById(UserId);

                csCustInfo.UserId = entUserInfo.UserId;
                csCustInfo.UserName = entUserInfo.UserName;
                csCustInfo.UserCode = entUserInfo.UserCode;
                csCustInfo.DepartId = entUserInfo.DepartId;
                csCustInfo.DepartName = entUserInfo.DepartName;
                csCustInfo.CityId = entUserInfo.CityId;
                csCustInfo.CityName = entUserInfo.CityName;
                csCustInfo.UserType = (int)entUserInfo.UserType;
                csCustInfo.CompanyId = entUserInfo.CompanyId;
                csCustInfo.CompanyName = entUserInfo.CompanyName;

                cs.Set(strKeyUser, csCustInfo);
            }

            return csCustInfo;
        }

        //*******************************************
        //功能概要       ：当前用户所具有权限的判断
        //参数説明       ：strPermit 要进行判断的权限
        //返回值説明     ：判断结果
        //功能詳細       ：当前用户所具有权限的判断
        //作成者         ：he.jifeng 
        //作成日         ：2010-03-02 
        //*******************************************
        public Boolean ContainsPermit(string strPermit)
        {
            //return true;
            if (UserType == 100) return true;

            //Cache服务类
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            //权限列表
            List<string> lsPermits;
            //用户权限列表键
            string strKeyUserPermits = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_PERMITS, UserId.ToString());
            //从Cache中查找
            lsPermits = cs.GetList<string>(strKeyUserPermits);
            //未命中
            //删除Cache中权限时机未明确，暂时先从新搜索数据库
            //if (!cs.Hit)
            if (true)
            {
                //用户权限业务处理类
                Chenxin.Lib.Passport.BLL.User_Permits bllUserPermits = new Chenxin.Lib.Passport.BLL.User_Permits();
                //用户权限列表
                lsPermits = bllUserPermits.GetList(UserId);
                //添加到Cache中
                cs.Set(strKeyUserPermits, lsPermits);
            }

            //列表为空
            if (lsPermits == null)
            {
                return false;
            }
            //循环查找权限列表
            foreach (string curPermit in lsPermits)
            {
                if (curPermit.ToLower() == strPermit.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取用户所操控的部门集合
        /// </summary>
        /// <returns></returns>
        public System.Data.DataSet GetUserControledDeparts()
        {
            System.Data.DataSet dsDeparts = null;
            //string strCachedKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_USER_CONTROLED_DEPARTS, UserId);

            //Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            //dsDeparts = cs.GetDataSet(strCachedKey);
            //if (!cs.Hit)
            //{
                string strPermitCode = "user.controled.departs.all";
                string strPermitCode1 = "user.controled.departs.company";
                string strwhere ="";
                if (ContainsPermit(strPermitCode))
                {
                    //strwhere = string.Format("(CompanyId = '{0}' AND (DepartProperty = 500 OR DepartProperty = 600))", _companyId);
                    strwhere = "(DepartProperty = 500 OR DepartProperty = 600)";
                    dsDeparts = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetList(strwhere);
                }
                else if (ContainsPermit(strPermitCode1))
                {
                    strwhere = string.Format("(CompanyId = '{0}' AND (DepartProperty = 500 OR DepartProperty = 600))", _companyId);
                    dsDeparts = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetList(strwhere);
                }
                else
                {
                    dsDeparts = Chenxin.Lib.Hr.DAL.Structures.GetUserControledDeparts(UserId);
                }
            //    if (dsDeparts != null) cs.Set(strCachedKey, dsDeparts, DateTime.Now.AddDays(1));
            //}

            return dsDeparts;
        }
    }
}
